package com.ws.lv.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.lv.bean.Order_info;
import com.ws.lv.service.Order_infoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/order_info")
public class Order_infoController {

    @Autowired
    Order_infoService order_infoService;

    //去订单信息管理页面
    @RequestMapping("/selectOrder_info")
    public String selectOrder_info() {
        return "adminpage/order_info/list";
    }
    //去添加订单信息页面
    @RequestMapping("/toAddOrder_info")
    public String toAddOrder_info() {
        return "adminpage/order_info/add";
    }
    //去修改订单信息页面
    @RequestMapping("/toUpdateOrder_info")
    public String toUpdateOrder_info() {
        return "adminpage/order_info/update";
    }

    //查询订单信息
    @RequestMapping("/selectOrder_infos")
    public String selectOrder_infos(String user_name, String route_name, Model model,Integer currentPage) {
        PageHelper pageHelper = new PageHelper();//分页帮助类
        if (currentPage == null){
            pageHelper.startPage(1, 5);
        }else {
            pageHelper.startPage(currentPage, 5);
        }
        List<Order_info> orderInfos = order_infoService.selectOrder_infos(user_name, route_name);
        PageInfo pageInfo = new PageInfo(orderInfos);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("user_name", user_name);
        model.addAttribute("route_name", route_name);
        System.out.println(orderInfos);
        return "adminpage/order_info/list";
    }

    //修改订单状态
    @RequestMapping("/updateOrder_infoByState")
    public String updateOrder_infoByState(int order_info_id) {
        System.out.println("修改订单状态");
        int i = order_infoService.updateOrder_infoByState(order_info_id);
        return "forward:/order_info/selectOrder_infos";
    }

    //删除订单
    @RequestMapping("/deleteOrder_infos")
    public String deleteOrder_infos(String orderInfoIds) {
        System.out.println("删除订单");
        if (orderInfoIds != null && orderInfoIds.trim().length() > 0){
            String[] ids = orderInfoIds.split(",");
            ArrayList<Integer> orderInfoIdList = new ArrayList<>();
            for (String id : ids) {
                orderInfoIdList.add(Integer.parseInt(id));
            }
            int i = order_infoService.deleteOrder_infos(orderInfoIdList);
        }
        return "forward:/order_info/selectOrder_infos";
   }
}
